$(function () {
    //ajax get请求
    $('.ajax-get').click(function () {
        var target;
        if ($(this).hasClass('confirm')) {
            if (!confirm('确认要执行该操作吗?')) {
                return false;
            }
        }
        if ((target = $(this).attr('href')) || (target = $(this).attr('url'))) {
            $.get(target).done(function (data) {
                switch (data.code) {
                case 0:
                    if (data.msg) {
                        updateAlert(data.msg, 0);
                        if (data.url) {
                            setTimeout(function () {
                                $btn.button('reset');
                                location.href = data.url;
                            }, data.wait * 1000);
                        } else {
                            $btn.button('reset');
                        }
                    } else {
                        updateAlert(data.data, 1, 3);
                        $btn.button('reset');
                    }
                    break;
                case 1:
                    if (data.msg) {
                        updateAlert(data.msg + ' 页面即将自动跳转~', 1);
                        setTimeout(function () {
                            $btn.button('reset');
                            location.href = data.url;
                        }, data.wait * 1000);
                    } else {
                        updateAlert(data.data, 1, 3);
                        $btn.button('reset');
                    }
                    break;
                default:
                    updateAlert(data.data, 1, 3);
                    $btn.button('reset');
                }
            });
        }
        return false;
    });

    //ajax post submit请求
    $('.ajax-post').click(function () {
        var target, query, form;
        var target_form = $(this).attr('target-form');
        var nead_confirm = false;
        if (($(this).attr('type') == 'submit') || (target = $(this).attr('href')) || (target = $(this).attr('url'))) {
            form = $('.' + target_form);
            if ($(this).attr('hide-data') === 'true') { //无数据时也可以使用的功能
                form = $('.hide-data');
                query = form.serialize();
            } else if (form.get(0) == undefined) {
                return false;
            } else if (form.get(0).nodeName == 'FORM') {
                if ($(this).hasClass('confirm')) {
                    if (!confirm('确认要执行该操作吗?')) {
                        return false;
                    }
                }
                if ($(this).attr('url') !== undefined) {
                    target = $(this).attr('url');
                } else {
                    target = form.get(0).action;
                }
                query = form.serialize();
            } else if (form.get(0).nodeName == 'INPUT' || form.get(0).nodeName == 'SELECT' || form.get(0).nodeName == 'TEXTAREA') {
                form.each(function (k, v) {
                    if (v.type == 'checkbox' && v.checked == true) {
                        nead_confirm = true;
                    }
                })
                if (nead_confirm && $(this).hasClass('confirm')) {
                    if (!confirm('确认要执行该操作吗?')) {
                        return false;
                    }
                }
                query = form.serialize();
            } else {
                if ($(this).hasClass('confirm')) {
                    if (!confirm('确认要执行该操作吗?')) {
                        return false;
                    }
                }
                query = form.find('input,select,textarea').serialize();
            }
            var $btn = $(this).button('loading');
            $.post(target, query).done(function (data) {
                switch (data.code) {
                case 0:
                    if (data.msg) {
                        updateAlert(data.msg, 0);
                        if (data.url) {
                            setTimeout(function () {
                                $btn.button('reset');
                                location.href = data.url;
                            }, data.wait * 1000);
                        } else {
                            $btn.button('reset');
                        }
                    } else {
                        updateAlert(data.data, 1, 3);
                        $btn.button('reset');
                    }
                    break;
                case 1:
                    if (data.msg) {
                        updateAlert(data.msg + ' 页面即将自动跳转~', 1);
                        setTimeout(function () {
                            $btn.button('reset');
                            location.href = data.url;
                        }, data.wait * 1000);
                    } else {
                        updateAlert(data.data, 1, 3);
                        $btn.button('reset');
                    }
                    break;
                default:
                    updateAlert(data.data, 1, 3);
                    $btn.button('reset');
                }
            });
        }
        return false;
    });

    window.closeAlert = function () {
        $('#alert-mask').removeClass('show');
    }
    window.updateAlert = function (text, type, auto_close) {
        window.clearInterval(st);
        var alertMask = $('#alert-mask');
        alertMask.addClass('show');
        alertMask.find('.alert-content').html(text);
        switch (type) {
        case 0:
            alertMask.find('.alert').addClass('alert-danger').removeClass('alert-success').removeClass('alert-warning');
            break;
        case 1:
            alertMask.find('.alert').addClass('alert-success').removeClass('alert-danger').removeClass('alert-warning');
            break;
        default:
            alertMask.find('.alert').addClass('alert-warning').removeClass('alert-success').removeClass('alert-danger');
        }
        if (auto_close) {
            var setTimeoutEndTime = auto_close;
            alertMask.find('.alert-auto-close').show().find('span').html(auto_close);
            this.setTimeout(function () {
                alertMask.removeClass('show');
            }, auto_close * 1000);
            var st = setInterval(function () {
                setTimeoutEndTime = setTimeoutEndTime - 1;
                alertMask.find('.alert-auto-close').find('span').html(setTimeoutEndTime);
                if (setTimeoutEndTime == 0) {
                    window.closeAlert();
                    window.clearInterval(st);
                }
            }, 1000);
        } else {
            alertMask.find('.alert-auto-close').hide();
        }
    };
});